package com.sinoglobal.sinostore.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.bean.BaseVo;
import com.sinoglobal.sinostore.listener.OnAntiViolenceClickListener;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.TextUtil;
import com.sinoglobal.sinostore.utils.ValidUtil;

/**
 * @author LK
 * @createdate 2014-9-11 下午2:20:08
 * @Description: TODO(添加收货地址)
 */
public class AddAddressActivity extends StateBaseActivity {
    private RelativeLayout rlAddress;//添加收货地址
    private TextView address;//省市区
    private EditText specificAddress;//详细地址
    private EditText customerName;//收货人姓名
    private EditText phoneNum;//电话号码
    String strAddress = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        titleView.setText("添加收货地址");
        titleRightText.setText("保存");
        setContentView(R.layout.shop_activity_addaddress_layout);
        openChenJInShi();
        //初始化数据
        initView();
        //选择城市之后显示在界面上
        getData();

    }

    private void getData() {
        Intent intent = getIntent();
        strAddress = intent.getStringExtra("address");
        if (null != strAddress) {
            address.setText((strAddress));
        }
    }

    /**
     * @author lk
     * @createdate 2014-9-11 下午2:56:03
     * @Description: (初始化控件)
     */
    private void initView() {
        rlAddress = (RelativeLayout) this.findViewById(R.id.rl_id3);
        address = (TextView) this.findViewById(R.id.address);
        specificAddress = (EditText) this.findViewById(R.id.specificAddress);
        customerName = (EditText) this.findViewById(R.id.customerName);
        phoneNum = (EditText) this.findViewById(R.id.phoneNum);
        baseButRight.setOnClickListener(clickListener);
        rlAddress.setOnClickListener(clickListener);
    }


    /**
     * @author lk
     * @createdate 2014-9-21 上午10:47:01
     * @Description: (添加收货地址)
     */
    @SuppressWarnings("unchecked")
    private void saveAddress() {
        String strAddress = address.getText().toString().trim();
        String strSpecifiAddress = specificAddress.getText().toString().trim();//详细收货地址
        String strCustomerName = customerName.getText().toString().trim();//收货人姓名
        String strPhoneNum = phoneNum.getText().toString().trim();
        //详细收货地址是否符合要求
        String strAddressMsg = ValidUtil.validAddress(strSpecifiAddress);
        if (TextUtil.stringIsNotNull(strAddressMsg)) {
            showShortToast(strAddressMsg);
            return;
        }
        if (strSpecifiAddress.length() < 2 || strSpecifiAddress.length() > 60) {
            showShortToast("详细地址仅支持2~60个字符");
            return;
        }
        //收货人姓名判断是否符合要求
        String checkNameMsg = ValidUtil.validPostName(strCustomerName);
        if (TextUtil.stringIsNotNull(checkNameMsg)) {
            showShortToast(checkNameMsg);
            return;
        }
        //验证手机号是否符合要求
        String msg = ValidUtil.validPhone(strPhoneNum);
        if (TextUtil.stringIsNotNull(msg)) {
            showShortToast(msg);
            return;
        }
        final ProgressDialog dialog = new ProgressDialog(this);
        try {
            dialog.show();
        } catch (Exception e) {
        }
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("por", "5002");
        params.addQueryStringParameter("proIden", Constants.STOREFLAG);
        params.addQueryStringParameter("user_id", Constants.userCenterId);
        params.addQueryStringParameter("area", strAddress);
        params.addQueryStringParameter("address", strSpecifiAddress);
        params.addQueryStringParameter("connect_name", strCustomerName);
        params.addQueryStringParameter("connect_tel", strPhoneNum);
        httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                }
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                }
                String result = responseInfo.result;
                if (!TextUtils.isEmpty(result)) {
                    BaseVo entity = JSON.parseObject(result, BaseVo.class);
                    if (Constants.STORE_SUCCESS_CODE.equals(entity.getCode())) {
                        showShortToast("添加成功");
                        AddAddressActivity.this.finish();
                    } else if (Constants.STORE_ADDRESSOVER_CODE.equals(entity.getCode())) {
                        showShortToast("收货地址已达上限");
                    } else {
                        showShortToast("添加失败");
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private OnAntiViolenceClickListener clickListener = new OnAntiViolenceClickListener() {
        @Override
        public void onAVClick(View v) {
            int id = v.getId();
            if (id == R.id.rl_id3) {
                Intent intent = new Intent(AddAddressActivity.this, SelAddressActivity.class);
                intent.putExtra("address", strAddress);
                startActivity(intent);
                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                AddAddressActivity.this.finish();
            } else if (id == R.id.shop_base_but_right) {
                saveAddress();
            }
        }
    };
}
